package com.qn.pipeline2.filters;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.only4play.order.commons.filters.AbstractOrderFilter;
import com.qn.pipeline2.context.InstanceBuildContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * 处理器 - 输入数据校验
 *
 * @author QuNing
 * @date 2022/08/29
 */
public class InputDataPreCheckerFilter extends AbstractOrderFilter<InstanceBuildContext> {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    protected void handle(InstanceBuildContext context) {
        logger.info("--输入数据校验--");

        Map<String, Object> formInput = context.getFormInput();

        if (MapUtil.isEmpty(formInput)) {
            context.setErrorMsg("表单输入数据不能为空");
            throw new RuntimeException("表单输入数据不能为空");
        }

        String instanceName = (String) formInput.get("instanceName");

        if (StrUtil.isBlank(instanceName)) {
            context.setErrorMsg("表单输入数据必须包含实例名称");
            throw new RuntimeException("表单输入数据必须包含实例名称");
        }
    }
}